package com.goockr.litepal;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.goockr.litepal.Tables.Book;

import java.util.List;

/**
 * Created by JJT-ssd on 2017/3/14.
 */

public class LitepalAdapter extends BaseAdapter {

    List<Book> books;
    public LitepalAdapter(List<Book> books)
    {
        this.books=books;
    }
    @Override
    public int getCount() {
        return books.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        View view=null;
        if (convertView==null)
        {
            view= LayoutInflater.from(parent.getContext()).inflate(R.layout.litepal_list_item,parent,false);
            viewHolder=new ViewHolder();
            viewHolder.tv_BookName= (TextView)view.findViewById(R.id.tv_BookName);
            viewHolder.tv_Author= (TextView)view.findViewById(R.id.tv_Author);
            view.setTag(viewHolder);
        }else
        {
            view=convertView;
            viewHolder=(ViewHolder) view.getTag();
        }
        viewHolder.tv_BookName.setText(books.get(position).getBookName());
        viewHolder.tv_Author.setText(books.get(position).getAuthor());

        return view;
    }

    class ViewHolder{
        TextView tv_BookName;
        TextView tv_Author;
    }
}
